package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.LinksCategories;

public class GenericLinkCategoriesDAO extends AutoKeys implements
		org.jetnuke.dao.LinkCategoriesDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericLinkCategoriesDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DowloadCategoriesModel#retrieveByPK(int)
	 */
	public LinksCategories retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkCatModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		LinksCategories s = new LinksCategories();

		if (rs.next()) {
			fillLinkCatFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DowloadCategoriesModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkCatModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	private void fillLinkCatFromResultSet(LinksCategories s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillLinkCatFromResultSet(LinksCategories, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setCID(rs.getInt("CID"));
		s.setCDESCRIPTION(rs.getString("CDESCRIPTION"));
		s.setPARENTID(rs.getInt("PARENTID"));
		s.setTITLE(rs.getString("TITLE"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillLinkCatFromResultSet(LinksCategories, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			LinksCategories u = new LinksCategories();
			fillLinkCatFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DowloadCategoriesModel#addNew(org.jetnuke.entities.DownloadsCategories)
	 */
	public int addNew(LinksCategories dc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(LinksCategories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("LinkCatModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("LinkCategoriesModel.lastGeneratedLinkCategoryID"));
		initNewLinkCat(dc, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(LinksCategories) - end"); //$NON-NLS-1$
		}
		return id;

	}

	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("LinkCatModel.count_all"));

		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return r;

	}

	/**
	 * @param dc
	 * @param p
	 */
	private void initNewLinkCat(LinksCategories dc, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewLinkCat(LinksCategories, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, dc.getCDESCRIPTION());
		p.setString(2, dc.getTITLE());
		p.setInt(3, dc.getPARENTID());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewLinkCat(LinksCategories, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DowloadCategoriesModel#update(org.jetnuke.entities.DownloadsCategories)
	 */
	public void update(LinksCategories dc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(LinksCategories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("LinkCatModel.update");
		p.setString(1, dc.getCDESCRIPTION());
		p.setString(2, dc.getTITLE());
		p.setInt(3, dc.getPARENTID());
		p.setInt(4, dc.getCID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(LinksCategories) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadCategoriesModel#selectAllOrderTitleAsc()
	 */
	public List selectAllOrderTitleAsc() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTitleAsc() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("LinkCatModel.selectAllOrderTitleAsc"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTitleAsc() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadCategoriesModel#selectByParent(int)
	 */
	public List selectByParent(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByParent(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("LinkCatModel.selectByParent"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByParent(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.LinkCategoriesModel#selectAllOrderCID()
	 */
	public List selectAllOrderCID() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderCID() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkCatModel.selectAllOrderCID"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderCID() - end"); //$NON-NLS-1$
		}
		return list;
	}
}
